﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugareUtilitati : UserControl
    {
        private Control redControl;
        string CNP;
        string limba;

        public AdaugareUtilitati(string limba, string CNP)
        {
            InitializeComponent();
            Rename(limba);
            this.CNP = CNP;
            this.limba = limba;
        }

        private void btAdauagaUtilitati_Click(object sender, EventArgs e)
        {
            if (redControl != null)
                changeColor(redControl);

            Utilitati ut = new Utilitati();

            ut.IdPacient = CNP;
            ut.Data = dtUtilitati.Value;
            if (cbManevrare.Checked)
                ut.ManevrareTvRadio = "DA";
            else
                ut.ManevrareTvRadio = "NU";

            if (cbSertare.Checked)
                ut.InchisDeschisSertare = "DA";
            else
                ut.InchisDeschisSertare = "NU";

            if (cbUsaFereastra.Checked)
                ut.InchisDeschisUsaFereastra = "DA";
            else
                ut.InchisDeschisUsaFereastra = "NU";

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            toValidate.Add(dtUtilitati);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    redControl = tb;
                    MessageBox.Show(errors["CampIncorect"], "Error",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            //salvare in BD            
            if (isValid == true)
            {
                try
                {
                    PersistenceManager pm = PersistenceManager.Instance;
                    pm.Save(ut);
                    MessageBox.Show(errors["okBD"], "Information",
                                  MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show(errors["ErrorBD"], "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
